package org.spider;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.util.zip.GZIPInputStream;

public class DictDownload {

	public static void downloadSecl(final String sUrl, String path)
			throws MalformedURLException {
		int bytesum = 0;
		int byteread = 0;

		URL url = new URL(sUrl);
		String fileName;
		try {
			fileName = URLDecoder.decode(sUrl.split("[=&]")[1] + ".scel",
					"GB2312");
		} catch (UnsupportedEncodingException e1) {
			fileName = "sougou_" + System.currentTimeMillis() + ".scel";
		}
		fileName = path + "/" + fileName;
		System.out.println("fileName:" + fileName);
		try {
			URLConnection conn = url.openConnection();
			conn.addRequestProperty("referer", "http://pinyin.sogou.com/dict/");
			InputStream inStream = conn.getInputStream();
			FileOutputStream fs = new FileOutputStream(fileName);

			byte[] buffer = new byte[4096];
			while ((byteread = inStream.read(buffer)) != -1) {
				bytesum += byteread;
				fs.write(buffer, 0, byteread);
			}
			fs.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static BufferedReader getReader(File f)
			throws FileNotFoundException, IOException {
		BufferedReader reader = null;
		if (f.getName().endsWith(".gz")) {
			reader = new BufferedReader(new InputStreamReader(
					new GZIPInputStream(new FileInputStream(f))));
		} else {
			reader = new BufferedReader(new InputStreamReader(
					new FileInputStream(f)));
		}
		return reader;
	}

	public static void main(String[] argv) throws FileNotFoundException,
			IOException {
		String line;
		String path = "dict_urls.txt";
		BufferedReader reader = getReader(new File(path));
		while ((line = reader.readLine()) != null) {
			System.out.println(line);
			downloadSecl(line, "d:/scel");

		}

	}

}
